package com.simba.server.commands.impl.update;

import java.util.Map;
import com.simba.common.command.ICommand;
import com.simba.common.database.DbOperator;

public class RenewMachineInfoCmd implements ICommand
{
	public static final String NAME = "RenewMachineInfoCmd";
	private final String beginSql = "update tt_machine_info set beginTime=?,status=? where id=1;";
	private final String endSql = "update tt_machine_info set endTime=?,status=? where id=1;";
	private final Map<String, Object> paramMap;

	public RenewMachineInfoCmd(Map<String, Object> paramMap)
	{
		this.paramMap = paramMap;
	}

	@Override
	public String getName()
	{
		return RenewMachineInfoCmd.NAME;
	}

	@Override
	public Boolean run(Object params)
	{
		Boolean ret = false;

		if (!(params instanceof DbOperator))
		{
			return ret;
		}
		if (paramMap == null)
		{
			return ret;
		}
		try
		{
			DbOperator dbOperator = (DbOperator) params;

			Object beginTime = this.paramMap.get("beginTime");
			Object endTime = this.paramMap.get("endTime");

			if ((beginTime != null) && !beginTime.equals(""))
			{
				dbOperator.prepareStatement(beginSql);
				dbOperator.setString(1, beginTime.toString());
				dbOperator.setObject(2, this.paramMap.get("status"));
			}
			else if ((endTime != null) && !endTime.equals(""))
			{
				dbOperator.prepareStatement(endSql);
				dbOperator.setString(1, endTime.toString());
				dbOperator.setObject(2, this.paramMap.get("status"));
			}
			else
			{
				return false;
			}

			int res = dbOperator.executeUpdate();
			if (res > 0)
			{
				ret = true;
			}
			else
			{
				ret = false;
			}

		}
		catch (Exception ex)
		{
			ret = false;
		}
		return ret;
	}
}
